<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Category extends Controller_Admin_Main
{

    private $category;

    public function before()
    {
        parent::before();
        $this->category = Model_Category::factory('category');
    }

    public function action_add()
    {

        Assets::js('kindedit', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        $id = intval($this->request->param('id'));
        if ($_POST)
        {
            $this->save($_POST);
        }
        $category_list = $this->category->get_category_list();
        $category_option = $this->get_category_option($category_list, $id);
        $this->template->assign('category_option', $category_option);
        $this->template->view('category_add');
    }

    public function action_edit()
    {
        Assets::js('kindedit', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        $id = intval($this->request->param('id'));
        if ($_POST)
        {
            $this->save($_POST, array('id' => $id));
        }
        $category = $this->category->get_row(array('id' => $id));
        $this->template->assign('data', $category);

        $category_list = $this->category->get_category_list();
        $category_option = $this->get_category_option($category_list, $category['parent_id']);
        $this->template->assign('category_option', $category_option);
        $this->template->view('category_edit');
    }

    public function action_list()
    {
        $id = intval($this->request->param('id'));
        $category_list_temp = $this->category->get_category_list(array('parent_id' => $id));
        $category_list = array();
        if (Arr::is_array($category_list_temp))
        {
            foreach ($category_list_temp as $key => $value)
            {
                $parent = $this->category->get_row(array('id' => $value['parent_id']), 'name,parent_id');
                $parent = count($parent) > 0 ? $parent : array('name' => '无', 'parent_id' => 0);
                $value['double_parent_id'] = $parent['parent_id'];
                $value['has_child'] = $this->category->get_var(array('parent_id' => $value['id']), 'COUNT(*)');
                $value['parent_name'] = $parent['name'];
                $category_list[$key] = $value;
            }
        }
        unset($category_list_temp);
        if ($id != 0)
        {
            $parent_id = $this->category->get_row(array('id' => $id), 'parent_id');
            $this->template->assign('parent_id', $parent_id['parent_id']);
        }

        $this->template->assign('current_id', $id);
        $this->template->assign('category_list', $category_list);
        $this->template->view('category_list');
    }

    public function action_batch_position()
    {
        $id = intval($this->request->param('id'));
        $position_arr = isset($_POST['position']) ? $_POST['position'] : array();
        if (count($position_arr) > 0)
        {
            foreach ($position_arr as $key => $value)
            {
                $this->category->update(array('position' => $value), array('id' => $key));
            }
        }
        Message::set(Message::SUCCESS, '分类排序成功');
        $this->request->redirect('admin/category/list/' . $id);
        $this->template->view('category_edit');
    }

    public function action_delete()
    {
        $id = intval($this->request->param('id'));
        $category = $this->category->get_row(array('id' => $id), 'name,parent_id');
        if (!count($category) > 0)
        {
            Message::set(Message::ERROR, "\"$category[name]\"" . '--该分类不存在');
            $this->request->redirect('admin/category/list');
        }
        $has_child = $this->category->get_var(array('parent_id' => $id), 'COUNT(*)');
        if (!empty($has_child))
        {
            Message::set(Message::ERROR, "\"$category[name]\"" . '--该分类下有子分类，请先删除子分类');
            $this->request->redirect('admin/category/list/' . $category['parent_id']);
        }

        if (!$this->category->delete(array('id' => $id)))
        {
            Message::set(Message::SUCCESS, "\"$category[name]\"" . '--分类删除失败');
            $this->request->redirect('admin/category/list/' . $category['parent_id']);
        }
        Message::set(Message::SUCCESS, "\"$category[name]\"" . '--分类删除成功');
        $this->request->redirect('admin/category/list/' . $category['parent_id']);
    }

    public function action_left()
    {
        $left_arr[0]['name'] = '分类管理';
        $left_arr[0]['sub'] = array(
            array("name" => "添加分类", "url" => URL::site() . "admin/category/add", 'id' => 20000),
            array('name' => '分类列表', 'url' => URL::site() . 'admin/category/list', 'id' => 20002),
        );
        $this->template->assign('left_arr', $left_arr);
        $this->template->view('left');
    }

    public function action_createfile()
    {
        $id = intval($this->request->param('id'));
        $category_id_list = $this->category->get_all_children($id);
        $category_list = array();
        if (Arr::validate_arr($category_id_list))
        {
            foreach ($category_id_list as $key => $value)
            {
                $category_list[$value] = $this->category->get_row(array('id' => $value), 'name,parent_id,id,meta_keywords,meta_title,meta_description');
            }
        }

        $file_path = SYSTEM_CATEGORY_CACHE_PATH . 'category_' . $id . '.php';
        if (Cache::instance()->write($file_path, $category_list))
        {
            Message::success('写入成功');
            $this->request->redirect('admin/category/list');
        }
    }

    public function save($post, $where_arr = array())
    {
        $data = array();
        $data['name'] = Arr::get($post, 'name', '');
        $data['parent_id'] = Arr::get($post, 'category_id', '');
        $data['is_show'] = Arr::get($post, 'is_show', 1);
        $data['position'] = Arr::get($post, 'position', 9999);
        $data['update_time'] = Date::get_time();
        $data['meta_title'] = Arr::get($post, 'meta_title', $data['name']);
        $data['meta_keywords'] = Arr::get($post, 'meta_keywords', $data['name']);
        $data['meta_description'] = Arr::get($post, 'meta_description', $data['name']);
        $data['content'] = Arr::get($post, 'content', '');
        $id = 0;
        if (Arr::validate_arr($where_arr))
        {
            $id = $where_arr['id'];
        }
        if ($this->category->is_exist(array('name' => $data['name'], 'parent_id' => $data['parent_id']), $id))
        {
            Message::error('分类名称已经存在');
            $this->request->redirect('admin/category/add');
        }
        if (Arr::validate_arr($where_arr))
        {
            $id = $this->category->update($data, $where_arr);
            if (!empty($id))
            {
                Message::success(__('update success'));
                $this->request->redirect('admin/category/edit/' . $id);
            }
        }
        else
        {
            $data['add_time'] = Date::get_time();
            $id = $this->category->insert($data);
            Message::success(__('add success'));
            $this->request->redirect('admin/category/add/' . $data['parent_id']);
        }
    }

}

